package edu.unc.comp410.colemangeer.system;

/**
 * Stub for login use case
 */

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

import edu.unc.comp410.colemangeer.data.ICustomerGateway;
import edu.unc.comp410.colemangeer.model.Customer;

public class LoginService {
	
	private static Logger log = Logger.getLogger(LoginService.class);
	private final ICustomerGateway customerData;
	private Map<Integer, Customer> loggedInCustomers; 
	
	/**
	 * Class that knows who is logged in, and can log people in and out
	 */
	public LoginService(ICustomerGateway customerData){
		this.customerData = customerData;
		loggedInCustomers = new HashMap<Integer,Customer>();
	}
	
	public Customer logIn (String customerName, String encryptedPassword){
		Set<Customer> returnSet = customerData.getByName(customerName);
		if (returnSet.iterator().hasNext()){
			// assuming that the database forbids duplicate names....
			Customer loggedInCustomer = returnSet.iterator().next();
			logIn(loggedInCustomer);
			return loggedInCustomer;
		}
		else{
			log.warn("Can't find customer: " + customerName);
			return null;
		}
	}
	
	private boolean logIn(Customer c){
		log.warn ("logging in customer id " + c.getId());
		loggedInCustomers.put(c.getId(), c);
		return true;
	}
	public boolean logOut(Customer c){
		log.warn ("logging out customer id " + c.getId());
		loggedInCustomers.remove(c.getId());
		return true;
	}
	
	public boolean isCustomerLoggedIn(Customer c){
		log.warn ("checking login status of customer id " + c.getId());
		if(loggedInCustomers.get(c.getId()).equals(c)){
			log.warn ("customer id " + c.getId() + "is logged in");
			return true;
		}
		else{
			log.warn ("customer id " + c.getId() + "is  not logged in");
			return false;
		}
		
	}

}
